package com.qf.commons.mysql.config;

import com.qf.commons.mysql.plugin.PagePlugin;
import com.qf.commons.mysql.plugin.SQLPlugin;
import com.qf.commons.mysql.property.PluginConfigInfo;
import com.qf.commons.mysql.webintercepter.PageIntercepter;
import com.qf.commons.mysql.webintercepter.PageResponseBodyAdvice;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * description: MySQL的自动装配
 * author: Ken
 * 公众号：Java架构栈
 */
@Configuration
@MapperScan("com.qf.data.mapper")
@EnableTransactionManagement
@EnableConfigurationProperties(PluginConfigInfo.class)
public class MySQLAutoConfigraution {

    //注册MyBatis插件
    @Bean
    @ConditionalOnProperty(name = "qphone.plugin.sql.enable", havingValue = "true", matchIfMissing = false)
    public SQLPlugin getSQLPlugin(){
        return new SQLPlugin();
    }

    //注册分页插件
    @Bean
    @ConditionalOnProperty(name = "qphone.plugin.page.enable", havingValue = "true", matchIfMissing = true)
    public PagePlugin getPagePlugin(){
        return new PagePlugin();
    }

    //注册分页拦截器
    @Bean
    @ConditionalOnProperty(name = "qphone.plugin.page.enable", havingValue = "true", matchIfMissing = true)
    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
    public PageIntercepter getPageIntercepter(){
        return new PageIntercepter();
    }

    @Bean
    @ConditionalOnProperty(name = "qphone.plugin.page.enable", havingValue = "true", matchIfMissing = true)
    public PageResponseBodyAdvice getPageResponseBodyAdvice(){
        return new PageResponseBodyAdvice();
    }
}
